import { Component, OnInit } from "@angular/core";
import { AxiosService } from "../../axios.service";
import { NzIconService } from "ng-zorro-antd/icon";

@Component({
  selector: "app-zhijvleixingset",
  templateUrl: "./zhijvleixingset.component.html",
  styleUrls: ["./zhijvleixingset.component.scss"],
})
export class ZhijvleixingsetComponent implements OnInit {
  type = undefined;
  // type1=this.type;
  time = undefined;
  value1;
  value2;
  value3;
  value4;

  add1;
  add2;

  timeList = [];
  typeList = [];

  timeSearch;
  typeSearch;

  typeFiler = [];
  timeFiler = [];

  listOfData = [];
  dataList = [];
  isVisible;
  timeAdd;
  typeAdd;
  zjtype;
  timetype;

  constructor(public axios: AxiosService, private iconService: NzIconService) {}

  ngOnInit(): void {
    this.queryData();
  }

  queryData() {
    this.axios
      .axiosPost("QueryPaibanInfo", {
        mode: this.zjtype,
        timetype: this.timetype,
      })
      .then((data) => {
        if (data) {
          this.listOfData = [];
          console.log(data);
          // this.listOfData = data['order'];
          console.log(data["order"]);
          for (let i = 0; i < data["order"].length; i++) {
            for (let j = 0; j < data["order"][i]["type"].length; j++) {
              let listdata = {
                mode: data["order"][i]["mode"],
                type: data["order"][i]["type"][j]["type"],
                time: data["order"][i]["type"][j]["time"],
              };
              this.listOfData.push(listdata);
            }
          }
          this.dataList = this.listOfData;
          console.log(this.listOfData);

          // 表单下拉框列表
          for (let i = 0; i < this.listOfData.length; i++) {
            this.typeList.push(this.listOfData[i].mode);
            this.unique(this.typeList);
            // console.log(this.listOfData[i].type);
            this.timeList.push(this.listOfData[i].type);
            this.unique(this.timeList);
          }
          // console.log(this.timeList);
          // console.log(this.typeList);

          
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }

  edit(data) {
    this.type = data.mode;
    this.value1 = data.time[0]["value"];
    this.value2 = data.time[1]["value"];
    this.value3 = data.time[2]["value"];
    this.value4 = data.time[3]["value"];
    this.time = data.type;
  }

  clear() {
    this.type = undefined;
    this.value1 = "";
    this.value2 = "";
    this.value3 = "";
    this.value4 = "";
    this.time = undefined;
  }

  save() {
    this.axios
      .axiosPost("AddPaibanInfo", {
        mode: this.type,
        timetype: this.time,
        name1: "design",
        newValue1: this.value1,
        name2: "work",
        newValue2: this.value2,
        name3: "workagain",
        newValue3: this.value3,
        name4: "zuzhuang",
        newValue4: this.value4,
      })
      .then((data) => {
        if (data) {
          this.clear();
          this.queryData();
          console.log(data);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }

  // 新增治具
  addType() {
    this.typeAdd = true;
    this.type = undefined;
  }
  cancelType() {
    this.typeAdd = false;
  }
  okType() {
    if (this.add2 == undefined) {
      alert("新增類型不可爲空");
    } else {
      console.log(this.add2);
      this.typeList.push(this.add2);
      this.unique(this.typeList);
      this.type = this.add2;
      this.typeAdd = false;
      console.log(this.unique(this.typeList));
    }
    this.add2 = undefined;
  }

  // 新增工時
  addTime() {
    this.timeAdd = true;
    this.time = undefined;
  }
  cancelTime() {
    this.timeAdd = false;
  }
  okTime() {
    if (this.add1 == undefined) {
      alert("新增類型不可爲空");
    } else {
      this.timeList.push(this.add1);
      this.unique(this.timeList);
      this.time = this.add1;
      this.timeAdd = false;
      console.log(this.timeList);
    }
    this.add1 = undefined;
    this.time = undefined;
  }

  // 治具搜索----重置
  reset1() {
    // console.log(this.typeSearch)
    // this.typeSearch = '';
    // this.dataList=this.listOfData
    this.zjtype = "";
    this.type = this.zjtype;
    this.queryData();
  }
  //治具搜索----搜索
  search1() {
    // let list=[];
    // for(let i=0;i<this.listOfData.length; i++){
    //   if(this.listOfData[i].mode == this.typeSearch){
    //     list.push(this.listOfData[i])
    //   }
    // }
    // this.dataList=list;
    // console.log(this.dataList)
    console.log(this.zjtype,this.timetype,);
    this.type = this.zjtype;
    this.queryData();
  }

  // 治具搜索----重置
  reset2() {
    // this.timeSearch = '';
    // this.dataList=this.listOfData
    this.timetype = "";
    this.time = this.timetype;
    this.queryData();
  }
  //治具搜索----搜索
  search2() {
    this.time = this.timetype;
    this.queryData();
    // let list=[];
    // for(let i=0;i<this.listOfData.length; i++){
    //   if(this.listOfData[i].type == this.timeSearch){
    //     list.push(this.listOfData[i])
    //   }
    // }
    // this.dataList=list;
    // console.log(this.dataList)
  }

  // 刪除彈窗
  showModal(data) {
    this.isVisible = true;
  }
  handleCancel() {
    this.isVisible = false;
  }
  handleOk() {
    this.axios
      .axiosPost("DeletePaibanInfo", { mode: this.type, timetype: this.time })
      .then((data) => {
        console.log(this.type);
        console.log(data);
        if (data["result"] === "ok") {
          this.isVisible = false;
          this.queryData();
          this.clear();
        }
      });
  }

  // 去重
  unique(arr) {
    arr.sort();
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] == arr[i + 1]) {
        arr.splice(i, 1);
      }
    }
    return arr;
  }
}
